package com.to8to.autotask.gcjd;

import static com.to8to.autotask.util.AutoTaskType.SET_KGDJ;
import static com.to8to.autotask.util.DBOperaType.UPDATE;
import static com.to8to.es.db.dao.DBTables.TABLE_ZB;
import static com.to8to.thrift.crm.commons.commonsConstants.NODE_ZB_KGDJ;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import org.elasticsearch.common.collect.Lists;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.to8to.autotask.resulthandler.FailHandler;
import com.to8to.autotask.rpc.SendMsg2ES;
import com.to8to.autotask.rpc.SendSMSClient;
import com.to8to.autotask.util.DBUtil;
import com.to8to.autotask.util.Utils;
import com.to8to.commons.utils.StringUtil;
import com.to8to.es.util.LogUtil;

/**
 * SetKGDJTask.java
 * 作 者：atom.liu
 * 版 本：v2.0
 * 日 期：2014年12月9日
 * 描 述：设置工程进度为开工大吉；并发送短信通知。
 */
public class SetKGDJTask implements Runnable
{
    private static final Logger logger = LoggerFactory.getLogger(SetKGDJTask.class);

    @Override
    public void run()
    {
        logger.info("设置开工大吉...开始");

        Connection connect = null;

        try
        {
            connect = DBUtil.getConnection(false);

            if (DBUtil.execTaskCountsAtToday(connect, SET_KGDJ) != 0)
                return;

            //合同开始日期为当天的，设置开工大吉，并发送短息通知
            List<Integer> list = update(connect);
            if (Utils.isNullOrEmpty(list))
            {
                logger.info("本次没有要设置开工大吉的中标记录");
                return;
            }

            logger.info("本次需要发送开工大吉短信的Zid：" + list);
            SendSMSClient.sendMsgWithZid(NODE_ZB_KGDJ, list);

            logger.info("设置开工大吉结束...");
        }
        catch (Throwable e)
        {
            logger.error("设置开工大吉失败！{} \n {}", e, LogUtil.logStackTrace(e));
            DBUtil.rollback(connect);
            if (e instanceof SQLException)
                FailHandler.handler(SetKGDJTask.class);
        }
        finally
        {
            DBUtil.releaseSource(null, null, connect);
        }
    }

    /**
     * 设置工程进度为开工大吉 
     */
    private List<Integer> update(Connection connect) throws SQLException
    {
        String today = Utils.getTodayStr();
        String querySql = "select * from to8to_jj_smt_zb where stime = '%s' and gcjddetail != 12";
        List<String> updateZidList = Lists.newArrayList();
        List<Integer> zidList = Lists.newArrayList();
        PreparedStatement stat = connect.prepareStatement(String.format(querySql, today));
        ResultSet result = stat.executeQuery();
        while (result.next())
        {
            String phone = result.getString("phone");
            if (StringUtil.isEmpty(phone))
                continue;
            int zid = result.getInt("zid");
            updateZidList.add(String.valueOf(zid));
            zidList.add(zid);
        }
        DBUtil.releaseSource(result, stat, null);

        if (zidList.size() == 0)
            return zidList;

        String sql = "update to8to_jj_smt_zb set gcjddetail = 12 where stime = '%s' and gcjddetail != 12";
        DBUtil.refresh2DB(connect, String.format(sql, today), SET_KGDJ, new SendMsg2ES(UPDATE, TABLE_ZB, updateZidList));

        return zidList;
    }
}
